<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +---------------------------------------------------------------------------
// | SWAN [ $_SWANBR_SLOGAN_$ ]
// +---------------------------------------------------------------------------
// | Copyright $_SWANBR_COPYRIGHT_$
// +---------------------------------------------------------------------------
// | Version  $_SWANBR_VERSION_$
// +---------------------------------------------------------------------------
// | Licensed ( $_SWANBR_LICENSED_URL_$ )
// +---------------------------------------------------------------------------
// | $_SWANBR_WEB_DOMAIN_$
// +---------------------------------------------------------------------------
 
/**
+------------------------------------------------------------------------------
* DB类example
+------------------------------------------------------------------------------
* 
* @package sw_db
* @version $_SWANBR_VERSION_$
* @copyright $_SWANBR_COPYRIGHT_$
* @author $_SWANBR_AUTHOR_$ 
+------------------------------------------------------------------------------
*/

require_once './core.php';

require_once PATH_SWAN_LIB . 'sw_db.class.php';

/*
+------------------------------------------------------------------
 * {{{ instance db
+------------------------------------------------------------------
*/

$__db = sw_db::factory();
/* }}}
+------------------------------------------------------------------
 * {{{ create table
+------------------------------------------------------------------
*/
/*
*/
$sql =<<<SQL
CREATE TABLE test(
 id int auto_increment not null primary key,
 username varchar(50) not null,	
 count int not null
);
SQL;
try {
	$__db->exec($sql);
} catch(sw_db_adapter_exception $e) {
	echo $e->getMessage();	
}
/* }}}
+------------------------------------------------------------------
 * {{{ quote and quote_into 
+------------------------------------------------------------------
*/
/*
$quote_1 = $__db->quote('sssss\'sssss');
P($quote_1);
$quote_2 = $__db->quote(array('a', 'b', 'c'));
P($quote_2);
$quote_3 = $__db->quote_into('where id in(?)', array('sasa1\'', 2, 3));
P($quote_3);
*/
/* }}}
+------------------------------------------------------------------
 * {{{  insert 
+------------------------------------------------------------------
*/
/*
for ($i = 0; $i<100; $i++) {
	$row = array(
		'username' => 'user' . $i,
		'count' => rand(0, 100),
	);
	$__db->insert('test', $row);
}
$last_insert_id = $__db->last_insert_id();
P($__db);
P($last_insert_id);
*/
/* }}}
+------------------------------------------------------------------
 * {{{ update  
+------------------------------------------------------------------
*/
/*

require_once PATH_SWAN_LIB . 'db/sw_db_expr.class.php';

$str = new sw_db_expr('count + 1');
$bind = array(
	'count' => $str,
	'username' => 'userupdate',
);

$where = $__db->quote_into('id >= ?', 2);
P($__db->update('test', $bind, $where));
*/
/* }}}
+------------------------------------------------------------------
 * {{{ 直接查询  
+------------------------------------------------------------------
*/
/*
$sql = $__db->quote_into('select * from test where id >= ?', 2);
//查出全部
$result = $__db->fetch_all($sql);
P($result);
$row = $__db->fetch_row($sql);
P($row);
$assoc = $__db->fetch_assoc($sql);
P($assoc);
$col = $__db->fetch_col($sql);
P($col);
$pairs = $__db->fetch_pairs($sql);
P($pairs);
$one = $__db->fetch_one($sql);
P($one);
*/
/* }}}
+------------------------------------------------------------------
 * {{{  delete 
+------------------------------------------------------------------
*/
/*
$where = $__db->quote_into('id > ?' , 2);
P($__db->delete('test', $where));
P($__db);
*/
/* }}}
+------------------------------------------------------------------
 * {{{ select  
+------------------------------------------------------------------
*/

$select = $__db->select();
$select->from('test AS t', 'username', 'swan_test')
	   ->where('count > ?', '3')
	   ->order('count desc')
	   ->limit(1);
$all = $__db->fetch_all($select);
P($all);
/* }}}
+------------------------------------------------------------------
 * {{{  
+------------------------------------------------------------------
*/
/* }}}
+------------------------------------------------------------------
 * {{{ get_profiler()  
+------------------------------------------------------------------
*/

$profiler = $__db->get_profiler();
$queries = $profiler->get_query_profiles();
foreach ($queries as $query) {
	$type = $query->get_query_type();
	if ($type >= 2) {
		P($query);	
	}
}
/* }}}
+------------------------------------------------------------------
 * {{{  
+------------------------------------------------------------------
*/
